<template>
  <div>
    <el-card
      shadow="always"
      :body-style="{ padding: '20px' }"
      style="margin: 20px"
    >
      <el-form label-width="20px" :inline="true" size="normal">
        <el-form-item>
          <el-input placeholder="角色名称" v-model="searchRoleName"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            size="small"
            icon="el-icon-search"
            @click="searchBtn"
            >搜索</el-button
          >
          <el-button size="small" @click="removeSearchBtn">清空</el-button>
        </el-form-item>
      </el-form>
      <div>
        <el-button type="primary" size="small" @click="addRoleBtn"
          >添加</el-button
        >
        <el-button
          type="danger"
          size="small"
          @click=""
          style="margin-left: 20px"
          >批量删除</el-button
        >
      </div>
      <el-table border stripe :data="roleList" style="margin: 20px 0">
        <el-table-column type="selection" width="50" align="center" />
        <el-table-column type="index" width="50" label="序号" align="center" />
        <el-table-column label="角色名称" prop="roleName"> </el-table-column>
        <el-table-column label="操作">
          <template v-slot="{ row }">
            <el-button
              type="info"
              size="mini"
              icon="el-icon-info"
              @click="toAuthAllot(row)"
            ></el-button>
            <el-button
              type="primary"
              size="mini"
              icon="el-icon-check"
              @click=""
            ></el-button>
            <el-button
              type="primary"
              size="mini"
              icon="el-icon-edit"
              @click="updataRoleBtn(row)"
            ></el-button>
            <el-button
              type="danger"
              size="mini"
              icon="el-icon-delete"
              @click="deleteRole(row.id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        :current-page.sync="currentPage"
        :page-sizes="[3, 5, 8, 10]"
        :page-size.sync="pageSize"
        :total="total"
        background
        layout=" sizes, prev, pager, next, jumper"
      >
      </el-pagination>
    </el-card>
    <!-- 添加或修改角色名弹框 -->
    <el-dialog title="添加用户" width="50%" :visible.sync="dialogVisible">
      <h4>请输入新名称</h4>
      <el-input
        v-model="newOrUpRoleinfo.roleName"
        placeholder="输入角色名称"
        size="small"
        clearable                                                                   
        style="margin:10px 0"
      ></el-input>
      <template #footer>
        <span>
          <el-button @click="roleSaveCancel">取消</el-button>
          <el-button type="primary" @click="roleSave">确定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import {
  reqGetRoleList,
  reqUpdateRole,
  reqAddRole,
  reqDeleteRole,
} from "@/api/auth/role";
export default {
  name: "Role",
  data() {
    return {
      currentPage: 1, //当前页码
      pageSize: 5, //当前连续页
      total: 0, //总条数
      roleList: [], //角色列表初始数据
      searchRoleName: "", //搜索框初始数据
      dialogVisible: false, //控制添加或修改角色名弹框显示与隐藏
      newOrUpRoleinfo: {
        //新增或修改角色名称初始值
        id: null,
        roleName: "",
      },
    };
  },
  methods: {
    //初始化获取角色列表数据
    async initRoleList() {
      try {
        const result = await reqGetRoleList(this.currentPage, this.pageSize);
        this.roleList = result.items;
        this.total = result.total;
      } catch (error) {
        this.$message({
          message: error.message,
          type: "error",
        });
      }
    },
    //点击搜索按钮事件回调函数
    async searchBtn() {
      try {
        const result = await reqGetRoleList(
          this.currentPage,
          this.pageSize,
          this.searchRoleName
        );
        this.roleList = result.items;
        this.total = result.total;
      } catch (error) {
        this.$message({
          message: error.message,
          type: "error",
        });
      }
    },
    //点击清除搜索按钮
    removeSearchBtn() {
      this.searchRoleName = "";
      this.initRoleList();
    },
    //点击新增角色按钮事件回调
    addRoleBtn() {
      this.dialogVisible = true;
      this.newOrUpRoleinfo = {
        id: null,
        roleName: "",
      };
    },
    //点击修改角色按钮事件回调
    updataRoleBtn(row) {
      this.dialogVisible = true;
      this.newOrUpRoleinfo = row;
    },
    //点击提交事件回调
    async roleSave() {
      try {
        if (this.newOrUpRoleinfo.id) {
          await reqUpdateRole(this.newOrUpRoleinfo);
          this.$message({
            message: "恭喜你，修改角色成功",
            type: "success",
          });
        } else {
          await reqAddRole(this.newOrUpRoleinfo);
          this.$message({
            message: "恭喜你，添加角色成功",
            type: "success",
          });
        }
      } catch (error) {
        this.$message({
          message: error.message,
          type: "error",
        });
      }
      this.initRoleList();
      this.dialogVisible = false;
    },
    //点击取消提价事件回调
    roleSaveCancel() {
      this.dialogVisible = false;
      this.$message({
        message: "取消设置成功",
        type: "warning",
      });
    },
    //删除某个角色事件回调
    async deleteRole(id){
      try {
        await reqDeleteRole(id)
        this.$message({
          message: '恭喜你，删除成功！',
          type: 'success'
        })
        this.initRoleList()
      } catch (error) {
        this.$message({
        message: error.message,
        type: "error",
      });
      }
    },
    //携带路由参数跳转权限分配页面
    toAuthAllot(row){
      this.$router.push({
        name:"Auth",
        query:{
          id:row.id,
          roleName:row.roleName
        }
      })
    }
  },
  mounted() {
    this.initRoleList();
  },
  watch: {
    currentPage() {
      this.initRoleList();
    },
    pageSize() {
      this.initRoleList();
    },
  },
};
</script>

<style lang="scss" scoped></style>
